<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import * as echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from './mixins/resize'

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '350px'
    },
    autoResize: {
      type: Boolean,
      default: true
    },
    chartData: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      chart: null
    }
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        this.setOptions(val)
      }
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
      this.setOptions(this.chartData)
    },
    setOptions(chartData = {}) {
      // 兼容两种数据格式：原始的{expectedData, actualData}和新的{labels, datasets}
      let xAxisData = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']; // 默认值
      let seriesData = [];
      
      if (chartData.labels && chartData.datasets) {
        // 新格式：{labels, datasets}
        xAxisData = chartData.labels;
        seriesData = chartData.datasets.map(dataset => ({
          name: dataset.label,
          itemStyle: {
            normal: {
              color: dataset.borderColor,
              lineStyle: {
                color: dataset.borderColor,
                width: 2
              }
            }
          },
          smooth: true,
          type: 'line',
          data: dataset.data,
          animationDuration: 2800,
          animationEasing: 'cubicInOut'
        }));
      } else if (chartData.expectedData || chartData.actualData) {
        // 原始格式：{expectedData, actualData}
        const { expectedData, actualData } = chartData;
        if (chartData.xAxis) {
          xAxisData = chartData.xAxis;
        }
        
        seriesData = [
          {
            name: 'expected',
            itemStyle: {
              normal: {
                color: '#FF005A',
                lineStyle: {
                  color: '#FF005A',
                  width: 2
                }
              }
            },
            smooth: true,
            type: 'line',
            data: expectedData,
            animationDuration: 2800,
            animationEasing: 'cubicInOut'
          }
        ];
        
        if (actualData && actualData.length > 0) {
          seriesData.push({
            name: 'actual',
            smooth: true,
            type: 'line',
            itemStyle: {
              normal: {
                color: '#3888fa',
                lineStyle: {
                  color: '#3888fa',
                  width: 2
                },
                areaStyle: {
                  color: '#f3f8ff'
                }
              }
            },
            data: actualData,
            animationDuration: 2800,
            animationEasing: 'quadraticOut'
          });
        }
      }
      
      this.chart.setOption({
        xAxis: {
          data: xAxisData,
          boundaryGap: false,
          axisTick: {
            show: false
          }
        },
        grid: {
          left: 10,
          right: 10,
          bottom: 20,
          top: 30,
          containLabel: true
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'cross'
          },
          padding: [5, 10],
          formatter: function(params) {
            // 检查是否有监测点名称数据
            if (chartData.pointNames && chartData.pointNames.length > 0) {
              const index = params[0].dataIndex;
              const pointName = chartData.pointNames[index] || '未知监测点';
              let result = params[0].axisValue + '<br/>';
              params.forEach(param => {
                result += param.marker + ' ' + param.seriesName + ': ' + param.value + '<br/>';
              });
              result += '监测点: ' + pointName;
              return result;
            }
            // 默认tooltip格式
            return params[0].axisValue;
          }
        },
        yAxis: {
          axisTick: {
            show: false
          }
        },
        legend: {
          data: seriesData.map(item => item.name)
        },
        series: seriesData
      })
    }
  }
}
</script>
